//
//  LandingVC.m
//  豆瓣
//
//  Created by lanou3g on 15/12/9.
//  Copyright © 2015年 HaiTeng. All rights reserved.
//

#import "LandingVC.h"
#import "RegisteredVC.h"

@interface LandingVC ()

@end

@implementation LandingVC



- (void)loadView
{
    self.landingView = [[LandingView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    self.view = self.landingView;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
   
    [self setbackBarButton];
    [self setLogingButton];
    [self setRegisteredButton];
    
    
  
}



#pragma mark 返回按钮
- (void)setbackBarButton
{
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backBarButtonAction:)];

    

}

- (void)backBarButtonAction:(UIBarButtonItem *)sneder
{
    [self dismissViewControllerAnimated:YES completion:nil];
}




#pragma mark 登陆按钮,及事件
- (void)setLogingButton
{
    [self.landingView.logingButton addTarget:self action:@selector(logingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)logingButtonAction:(UIButton *)sender
{
   
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *passWord = [ user objectForKey:@"userPassWord"];
    NSString *name = [user objectForKey:@"userName"];


        
        if ([self.landingView.nameTextField.text isEqualToString:name] && [self.landingView.passwordTextField.text isEqualToString:passWord]) {
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆成功" preferredStyle:UIAlertControllerStyleAlert];
            
            [user setObject:@"YES" forKey:@"State"];
            [alertC addAction:[UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                
                [self dismissViewControllerAnimated:YES completion:nil];
            }]];
            
            
            [self presentViewController:alertC animated:YES completion:nil];
            
            
            
        }else{
            
            UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误,请重新输入" preferredStyle:UIAlertControllerStyleAlert];
            
            
            [alertC addAction:[UIAlertAction actionWithTitle:@"好吧" style:UIAlertActionStyleDestructive handler:nil]];
            
            
            [self presentViewController:alertC animated:YES completion:nil];
        }
        
        

    
    

    
}


#pragma mark 注册按钮,及事件

- (void)setRegisteredButton
{
    [self.landingView.registeredButton addTarget:self action:@selector(registeredButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)registeredButtonAction:(UIButton *)sender
{
    NSLog(@"注册按钮");
    RegisteredVC *registeredVC = [[RegisteredVC alloc] init];
    registeredVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:registeredVC animated:YES];
    
}




@end
